/**
 * Class
 */
package org.traffometer.common.data;

import org.traffometer.opendmtp.protocol.GPSException;
import org.traffometer.opendmtp.protocol.GeoEvent;
import org.traffometer.opendmtp.protocol.GeoPoint;
import org.traffometer.opendmtp.protocol.Packet;
import org.traffometer.opendmtp.protocol.PayloadTemplate;

/**
 * A TraffoPacket contains a custom packet defined by means of OpenDMTP.
 * 
 * @author Alexey R. Olkov
 * 
 */
public class TraffoPacket { // extends somethingOrOther

  // /////
  // Temporarily changed GeoPoint into Point so things will compile, allowing
  // me to test the build scripts. Alexei, feel free to go back to GeoPoint
  // when you work on this class.
  // /////

  protected GeoPoint point1, point2;
  // protected Point point1, point2;
  protected String vehicleType;
  protected Packet packet = null;

  /**
   * Creates a traffopacket with given parameters.
   * 
   * @param point1 First GeoPoint
   * @param point2 Second GeoPoint
   * @param vehicleType String vehicle type
   * @throws GPSException if data are invalid.
   */
  // public TraffoPacket(Point point1,Point point2, String vehicleType) {
  public TraffoPacket(GeoPoint point1, GeoPoint point2, String vehicleType) throws GPSException {

    this.point1 = point1;
    this.point2 = point2;
    this.vehicleType = vehicleType;

    GeoEvent geoEvent = new GeoEvent();

    // CREATE GEO EVENT for the given GeoPoints
    geoEvent.setLatitude1(point1.getLatitude());
    geoEvent.setLongitude1(point1.getLongitude());
    geoEvent.setTimestamp1(point1.getFixtime());
    geoEvent.setLatitude2(point2.getLatitude());
    geoEvent.setLongitude2(point2.getLongitude());
    geoEvent.setTimestamp2(point2.getFixtime());
    geoEvent.setVehicle(vehicleType);

     
    if (geoEvent.isValid()) {
      packet = Packet.createClientEventPacket(geoEvent, Packet
          .getClientPayloadTemplate(Packet.PKT_CLIENT_CUSTOM_FORMAT_0));
    }
    else {
      throw new GPSException("The data is invalid");
    }
  }
/**
 * Creates a TraffoPacket from a byte array.
 * @param byteArray payload.
 */
  public TraffoPacket(byte[] byteArray) {
    this.packet = Packet.createClientPacket(Packet.PKT_CLIENT_CUSTOM_FORMAT_0, byteArray);
    PayloadTemplate templ = packet.getPayloadTemplate();

    this.point1 = packet.getPayload().readGPS(templ.getField(0).getLength());
    this.point1.setFixtime(packet.getPayload().readLong(templ.getField(1).getLength()));
    
    this.point2 = packet.getPayload().readGPS(templ.getField(2).getLength());
    this.point2.setFixtime(packet.getPayload().readLong(templ.getField(3).getLength()));
    
    this.vehicleType = packet.getPayload().readString(templ.getField(4).getLength());
   
  }

/**
 * Creates a TraffoPacket from an OpenDMTP packet.
 * @param packet a packet to use to initialize this TraffoPacket
 */
public TraffoPacket (Packet packet) {

  this.packet = packet;  
  PayloadTemplate templ = packet.getPayloadTemplate();

  this.point1 = packet.getPayload().readGPS(templ.getField(0).getLength());
  this.point1.setFixtime(packet.getPayload().readLong(templ.getField(1).getLength()));
  
  this.point2 = packet.getPayload().readGPS(templ.getField(2).getLength());
  this.point2.setFixtime(packet.getPayload().readLong(templ.getField(3).getLength()));
  
  this.vehicleType = packet.getPayload().readString(templ.getField(4).getLength());
  
}
  /**
   * Returns the DMTP packet.
   * 
   * @return packet
   */
  public Packet getPacket() {
    return this.packet;
  }

  /**
   * Returns a latitude of the fist point.
   * 
   * @return latitude
   */
  public double getLatitude1() {
    return this.point1.getLatitude();
  }

  /**
   * Returns a latitude of the second point.
   * 
   * @return latitude
   */
  public double getLatitude2() {
    return this.point2.getLatitude();
  }

  /**
   * Returns a longitude of the fist point.
   * 
   * @return longitude
   */
  public double getLongitude1() {
    return this.point1.getLongitude();
  }

  /**
   * Returns a longitude of the second point.
   * 
   * @return longitude
   */
  public double getLongitude2() {
    return this.point2.getLongitude();
  }

  /**
   * Returns a timestamp of the fist point.
   * 
   * @return long timestamp
   */
  public long getTimeStamp1() {
    return this.point1.getFixtime();
  }

  /**
   * Returns a timestamp of the second point.
   * 
   * @return tlong imestamp
   */

  public long getTimeStamp2() {
    return this.point2.getFixtime();
  }

  /**
   * Returns the vehicle type associated with this packet.
   * 
   * @return String representing the vehicle type.
   */
  public String getVehicleType() {
    return vehicleType;
  }

  /**
   * Puts the packet's data into a readable string.
   * 
   * @return String.
   */
  public String toString() {
    return "( " + this.getLatitude1() + " , " + this.getLongitude1() + " at "
        + this.getTimeStamp1() + ") - " + "( " + this.getLatitude2() + " , " + this.getLongitude2()
        + " at " + this.getTimeStamp2() + "): " + this.getVehicleType();
  }

  /**
   * Returns an array of bytes of a packet.
   * 
   * @return array fo bytes.
   */
  public byte[] getPacketBytes() {
    return this.getPacket().getPayloadBytes(0, this.getPacket().getPayloadLength());
  }
  // other methods needed based on reference config
}
